package com.ruitu.health_2.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.androidkun.xtablayout.XTabLayout;
import com.ruitu.arad.base.BaseActivity;
import com.ruitu.arad.base.base_list.ListBaseAdapter;
import com.ruitu.arad.util.ToastUtils;
import com.ruitu.health_2.R;
import com.ruitu.health_2.mvp.model.ModelModel;
import com.ruitu.health_2.mvp.presenter.ModelPresenter;
import com.ruitu.health_2.ui.adapter.CommonProListAdapter;
import com.ruitu.health_2.ui.adapter.HorizonArtManListAdapter;
import com.ruitu.health_2.ui.adapter.ModelActivityListAdpter;
import com.ruitu.health_2.ui.adapter.ModelArticleListAdapter;
import com.ruitu.health_2.ui.adapter.ModelBookListAdpter;
import com.ruitu.health_2.ui.adapter.ModelVideoListAdapter;
import com.ruitu.health_2.ui.common.VideoDetailActivity;
import com.ruitu.health_2.ui.fragment.ReadPartyBookFragment;
import com.ruitu.router_module.Router;
import com.ruitu.router_module.bean.article.Article;
import com.ruitu.router_module.bean.product.Activity;
import com.ruitu.router_module.bean.product.ArtMan;
import com.ruitu.router_module.bean.product.Book;
import com.ruitu.router_module.bean.product.ProHome;
import com.ruitu.router_module.bean.product.ReadPartyDetail;
import com.ruitu.router_module.bean.product.Video;
import com.ruitu.router_module.support.view.MeasuredNoScrollViewPager;
import com.ruitu.router_module.util.GlideUtil;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

// 读书会详情
public class ReadPartieDetailActivity extends BaseActivity<ModelPresenter.ReadPartieDetailP, ModelModel.ReadPartieDetailM> {
    private RecyclerView rcv_art_man;// 艺人
    private RecyclerView rcv_video;// 视频
    private RecyclerView rcv_article;// 文章
    private RecyclerView rcv_activity;// 活动
    private RecyclerView rcv_product;// 商品
    private RecyclerView rcv_book_list;// 书籍
    private XTabLayout tab;
    private ImageView iv_reco_book;// 阅读推荐
    private MeasuredNoScrollViewPager vp_pro;// 阅读状态

    private HorizonArtManListAdapter artManAdapter;
    private List<ArtMan> artManList = new ArrayList<>();
    private ModelVideoListAdapter videoAdapter;
    private List<Video> videoList = new ArrayList<>();
    private ModelArticleListAdapter articleAdapter;
    private List<Article> articleList = new ArrayList<>();
    private ModelActivityListAdpter activityAdpter;
    private List<Activity> activityList = new ArrayList<>();
    private CommonProListAdapter proAdapter;
    private List<ProHome> proList = new ArrayList<>();
    private ModelBookListAdpter bookAdapter;
    private List<Book> bookList = new ArrayList<>();

    private ReadPartyDetail detail;// 请求的读书会详情

    private String name = "读书会";// 传过来的读书会名称
    private String id = "";// 传过来的读书会id
    public static final String[] HOME_MODELS = {"在读", "未读", "已读"};
    private VpAdapter vpAdapter;

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_read_partie_detail;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        rcv_art_man = findViewById(R.id.rcv_art_man);
        rcv_video = findViewById(R.id.rcv_video);
        rcv_article = findViewById(R.id.rcv_article);
        rcv_activity = findViewById(R.id.rcv_activity);
        rcv_product = findViewById(R.id.rcv_product);
        rcv_book_list = findViewById(R.id.rcv_book_list);
        tab = findViewById(R.id.tab);
        iv_reco_book = findViewById(R.id.iv_reco_book);
        vp_pro = findViewById(R.id.vp_pro);

        Bundle bundle = getIntent().getExtras();
        if (null != bundle) {
            name = bundle.getString("name");
            id = bundle.getString("id");
        }

        // 艺人
        artManAdapter = new HorizonArtManListAdapter(this);
        LinearLayoutManager manager1 = new LinearLayoutManager(this);
        manager1.setOrientation(RecyclerView.HORIZONTAL);
        rcv_art_man.setLayoutManager(manager1);
        rcv_art_man.setAdapter(artManAdapter);

        // 视频
        videoAdapter = new ModelVideoListAdapter(this);
        GridLayoutManager manager2 = new GridLayoutManager(this, 2);
        rcv_video.setLayoutManager(manager2);
        rcv_video.setAdapter(videoAdapter);

        // 文章
        articleAdapter = new ModelArticleListAdapter(this);
        LinearLayoutManager manager3 = new LinearLayoutManager(this);
        rcv_article.setLayoutManager(manager3);
        rcv_article.setAdapter(articleAdapter);

        // 活动
        activityAdpter = new ModelActivityListAdpter(this);
        LinearLayoutManager manager5 = new LinearLayoutManager(this);
        rcv_activity.setLayoutManager(manager5);
        rcv_activity.setAdapter(activityAdpter);

        // 商品
        proAdapter = new CommonProListAdapter(this);
        GridLayoutManager manager6 = new GridLayoutManager(this, 2);
        rcv_product.setLayoutManager(manager6);
        rcv_product.setAdapter(proAdapter);
        for (int i = 0; i < 4; i++) {
            ProHome pro = new ProHome();
            pro.setName("商品名称" + i);
            pro.setPrice("" + i);
            pro.setPic("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1582106150394&di=bac72dafa07bd41888f4f60e5b7ba3dc&imgtype=0&src=http%3A%2F%2Fimg.zcool.cn%2Fcommunity%2F013abc574050576ac725ac3f46e91c.jpg");
            proList.add(pro);
        }
        proAdapter.setDataList(proList);

        // 书籍
        bookAdapter = new ModelBookListAdpter(this);
        LinearLayoutManager manager4 = new LinearLayoutManager(this);
        rcv_book_list.setLayoutManager(manager4);
        rcv_book_list.setAdapter(bookAdapter);
        for (int i = 0; i < 4; i++) {
            Book book = new Book();
            book.setBookName("书名" + i);
            book.setArtManName("作者" + i);
            book.setReadPartyName("读书会" + i);
            book.setBookCover("https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1734489807,1709236067&fm=26&gp=0.jpg");
            bookList.add(book);
        }
        bookAdapter.setDataList(bookList);

        // 3个tab
        for (int i = 0; i < 3; i++) {
            tab.addTab(tab.newTab().setText(HOME_MODELS[i]), i);
        }
        vp_pro.setOffscreenPageLimit(99);
        vp_pro.setScanScroll(false);
        vpAdapter = new VpAdapter(getSupportFragmentManager());
        vp_pro.setAdapter(vpAdapter);
        tab.setupWithViewPager(vp_pro);

        // 阅读推荐
        GlideUtil.loadImg("https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1734489807,1709236067&fm=26&gp=0.jpg", iv_reco_book);

        setHeadTitle(name);
        setListeners();

        reqData();
    }

    private void reqData() {
        p.reqReadPartieDetail(id);
    }

    @Override
    public void onReqComplete(int code, boolean isOk, Object data) {
        if (code == 1) {
            detail = (ReadPartyDetail) data;

            artManList = detail.getUmsArtManList();
            videoList = detail.getCmsVideoList();
            articleList = detail.getCmsArticleList();
            activityList = detail.getUmsActivityList();

            setDataToView();
        }
    }

    private void setDataToView() {
        artManAdapter.setDataList(artManList);
        videoAdapter.setDataList(videoList);
        articleAdapter.setDataList(articleList);
        activityAdpter.setDataList(activityList);
    }

    private void setListeners() {
        artManAdapter.setOnItemClickListener(new ListBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View itemView, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("id", artManList.get(position).getId());
                bundle.putString("name", artManList.get(position).getName());
                startActivity(ArtManDetailActivity.class, bundle);
            }
        });
        videoAdapter.setOnItemClickListener(new ListBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View itemView, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("videoId", videoList.get(position).getId());
                bundle.putString("videoUrl", videoList.get(position).getVideo());
                startActivity(VideoDetailActivity.class, bundle);
            }
        });
        articleAdapter.setOnItemClickListener(new ListBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View itemView, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("id", articleList.get(position).getId());
                try {
                    Router.startActivity(ReadPartieDetailActivity.this, "com.ruitu.health_1.ui.ArticleDetailsActivity", bundle);
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
        });
        activityAdpter.setOnItemClickListener(new ListBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View itemView, int position) {
                ToastUtils.showShortSafe("活动详情");
            }
        });
        bookAdapter.setOnItemClickListener(new ListBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View itemView, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("name", bookList.get(position).getBookName());
                bundle.putString("content", bookList.get(position).getContentmb());
                startActivity(BookDetailActivity.class, bundle);
            }
        });
    }

    private class VpAdapter extends FragmentPagerAdapter {

        public VpAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            if (position == 0) {// 在读
                return ReadPartyBookFragment.newInstance(id, "2");
            } else if (position == 1) {// 未读
                return ReadPartyBookFragment.newInstance(id, "3");
            } else if (position == 2) {// 已读
                return ReadPartyBookFragment.newInstance(id, "1");
            }
            return ReadPartyBookFragment.newInstance(id, "2");
        }

        @Override
        public int getCount() {
            return 3;
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return HOME_MODELS[position];
        }
    }
}